package edu.scau.mis.web.controller.person;

import edu.scau.mis.person.domain.Employees;
import edu.scau.mis.person.service.IEmployeesService;
import edu.scau.mis.core.domain.AjaxResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
/**
 * (Employees)表控制层
 *
 * @author CJW
 * @since 2024-03-29 14:24:15
 */
@RestController
@RequestMapping("employees")
public class EmployeesController {
    /**
     * 服务对象
     */
    @Autowired
    private IEmployeesService employeesService;

    /**
     * 查询员工列表
     *
     * @param employees 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    @ApiOperation("查询员工列表")
    public AjaxResult list(Employees employees) {
        return AjaxResult.success(this.employeesService.selectList(employees));
    }

    /**
     * 分页查询员工列表
     *
     * @param employees 筛选条件
     * @return 查询结果
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, Employees employees) {
        PageHelper.startPage(pageNum,pageSize);
        List<Employees> list = this.employeesService.selectList(employees);
        PageInfo<List> pageInfo = new PageInfo(list);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 通过主键查询单条员工数据
     *
     * @param id 主键
     * @return 单条员工
     */
    @GetMapping("{id}")
    @ApiOperation("通过ID查询单条员工数据")
    public AjaxResult getById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.employeesService.selectById(id));
    }

    /**
     * 新增员工
     *
     * @param employees 实体
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增员工")
    public AjaxResult add(@Valid @RequestBody Employees employees) {
        // 调用Service层进行数据库操作
        return AjaxResult.success(this.employeesService.insert(employees));
    }

    /**
     * 编辑员工
     *
     * @param employees 实体
     * @return 编辑结果
     */
    @PutMapping
    @ApiOperation("更新员工")
    public AjaxResult edit(@RequestBody Employees employees) {
        return AjaxResult.success(this.employeesService.update(employees));
    }

    /**
     * 批量删除员工
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/{id}")
    @ApiOperation("批量删除员工")
    public AjaxResult deleteById(@PathVariable Long[] id) {
        int rows = employeesService.deleteByIds(id);
        return rows > 0 ? AjaxResult.success("批量删除员工成功") : AjaxResult.error("批量删除员工失败");
    }
}
